package org.jeecg.modules.wangyue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.constant.IsPublishConstant;
import org.jeecg.modules.wangyue.constant.RoleConstant;
import org.jeecg.modules.wangyue.entity.SystemInfo;
import org.jeecg.modules.wangyue.entity.SystemInfoJs;
import org.jeecg.modules.wangyue.entity.UserBasic;
import org.jeecg.modules.wangyue.mapper.SystemInfoJsMapper;
import org.jeecg.modules.wangyue.mapper.SystemInfoMapper;
import org.jeecg.modules.wangyue.mapper.UserBasicMapper;
import org.jeecg.modules.wangyue.service.ISystemInfoJsService;
import org.jeecg.modules.wangyue.service.ISystemInfoService;
import org.jeecg.modules.wangyue.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 系统信息表
 * @Author: jeecg-boot
 * @Date:   2023-06-30
 * @Version: V1.0
 */
@Service
public class SystemInfoServiceImpl extends ServiceImpl<SystemInfoMapper, SystemInfo> implements ISystemInfoService {

    @Autowired
    private SystemInfoMapper systemInfoMapper;
    @Autowired
    private ISystemInfoJsService systemInfoJsService;
    @Autowired
    private UserBasicMapper userBasicMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean publishSystemInfo(SystemInfo systemInfo) {
        try{
            //查询全部账户userId
            QueryWrapper<UserBasic> userBasicQueryWrapper = new QueryWrapper<>();
            userBasicQueryWrapper.eq("del_flag", DelFlagConstant.normal);
            List<UserBasic> basicList = userBasicMapper.selectList(userBasicQueryWrapper);

            //向 表tb_system_info_js 中新增相应的数据
            if(oConvertUtils.isNotEmpty(basicList) && basicList.size() > 0){
                List<SystemInfoJs> systemInfoJsList = new ArrayList<>();
                for(UserBasic userBasic : basicList){
                    SystemInfoJs yhSystemInfoJs = new SystemInfoJs();
                    yhSystemInfoJs.setUserId(userBasic.getId());
                    yhSystemInfoJs.setSysInfoId(systemInfo.getId());
                    SystemInfoJs jsSystemInfoJs = new SystemInfoJs();
                    jsSystemInfoJs.setUserId(userBasic.getId());
                    jsSystemInfoJs.setSysInfoId(systemInfo.getId());

                    if(RoleConstant.all.equals(systemInfo.getPublishTo())){
                        yhSystemInfoJs.setUserType(RoleConstant.yonghu);
                        systemInfoJsList.add(yhSystemInfoJs);
                        jsSystemInfoJs.setUserType(RoleConstant.jishi);
                        systemInfoJsList.add(jsSystemInfoJs);
                    }else if(RoleConstant.yonghu.equals(systemInfo.getPublishTo())){
                        yhSystemInfoJs.setUserType(RoleConstant.yonghu);
                        systemInfoJsList.add(yhSystemInfoJs);
                    }else if(RoleConstant.jishi.equals(systemInfo.getPublishTo())){
                        jsSystemInfoJs.setUserType(RoleConstant.jishi);
                        systemInfoJsList.add(jsSystemInfoJs);
                    }
                }
                systemInfoJsService.saveBatch(systemInfoJsList, 10000);
            }

            //修改 表tb_system_info 该id数据的发布状态
            UpdateWrapper<SystemInfo> systemInfoUpdateWrapper = new UpdateWrapper<>();
            systemInfoUpdateWrapper.eq("id", systemInfo.getId());
            systemInfoUpdateWrapper.eq("del_flag", DelFlagConstant.normal);
            systemInfoUpdateWrapper.set("is_push", IsPublishConstant.yes);
            systemInfoUpdateWrapper.set("update_time", DateUtil.getTime("yyyy-MM-dd HH:mm:ss"));
            systemInfoMapper.update(null, systemInfoUpdateWrapper);

            return true;
        }catch (Exception e){
//            e.printStackTrace();
            throw new RuntimeException("系统消息发布操作失败！");
        }
    }



}
